package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Customer;
import com.service.CustomerService;
import com.service.impl.CustomerServiceImpl;

@WebServlet("/CustomerController")
public class CustomerController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public CustomerController() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");*/

		String method = request.getParameter("method");

		if ("addCustomer".equals(method))
			addCustomer(request, response);
		else if ("deleteCustomer".equals(method))
			deleteCustomer(request, response);
		else if ("updateCustomer".equals(method))
			updateCustomer(request, response);
		else if ("selectCustomer".equals(method))
			selectCustomer(request, response);
	}

	// 添加客户
	protected void addCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取页面信息
		String customerNo = request.getParameter("customerNo");
		String customerName = request.getParameter("customerName");
		String telephone = request.getParameter("telephone");
		String address = request.getParameter("address");

		// 添加数据
		Customer customer = new Customer();
		customer.setCustomerNo(customerNo);
		customer.setCustomerName(customerName);
		customer.setTelephone(telephone);
		customer.setAddress(address);

		CustomerService cd = new CustomerServiceImpl();
		boolean b = cd.addCustomer(customer);

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"添加成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"添加失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 删除客户
	protected void deleteCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		String resultJson = null;

		CustomerService cd = new CustomerServiceImpl();
		boolean b = cd.deleteCustomer(Integer.parseInt(id));
		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"删除成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"删除失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 修改客户
	protected void updateCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取页面信息
		String customerNo = request.getParameter("customerNo");
		String customerName = request.getParameter("customerName");
		String telephone = request.getParameter("telephone");
		String address = request.getParameter("address");
		String id = request.getParameter("id");

		// 添加数据
		Customer customer = new Customer();
		customer.setCustomerNo(customerNo);
		customer.setCustomerName(customerName);
		customer.setTelephone(telephone);
		customer.setAddress(address);

		CustomerService cd = new CustomerServiceImpl();
		boolean b = cd.updateCustomer(customer, Integer.parseInt(id));

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"修改成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"修改失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 查询客户
	protected void selectCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		int startPage = Integer.parseInt(page);
		int endRows = Integer.parseInt(rows);
		CustomerService cd = new CustomerServiceImpl();
		response.getWriter().print(
				cd.selectCustomer(key, type, startPage, endRows, sort, order));
		response.getWriter().flush();
	}
}
